﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Dting_Model
{
    #region //二进制序列化
    public class Serialize_Class  //序列化的位置务必加上[Serializable]
    {
        public int Serialize<T>(T source, string file_path)//序列化方法  
        {
            if (!typeof(T).IsSerializable)
            {
                //MessageBox.Show("类型必须是可序列化", "提示");
                return 1;
            }

            if (Object.ReferenceEquals(source, null))
            {
                //MessageBox.Show("实例不是相同的实例", "提示");
                return 2;
            }
            try
            {
                using (FileStream fs = new FileStream(file_path, FileMode.Create))//创建一个文件流，对文件进行写入
                {
                    BinaryFormatter bf = new BinaryFormatter();//使用CLR二进制格式器
                    bf.Serialize(fs, source); //序列化到硬盘
                    fs.Close();//关闭文件流
                }
                return 0;
            }
            catch
            {
                return 3;
            }
        }
        public T Deserialize<T>(string file_path)
        {
            try
            {
                using (FileStream fs = new FileStream(file_path, FileMode.Open))
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    return (T)binaryFormatter.Deserialize(fs);
                }
            }
            catch
            {
                return default(T);
            }
        }
    }
    #endregion

    #region  //ini类
    public class Model_ini_Class
    {
        public string _iniPath;  //ini连接路径

        [DllImport("kernel32")]//返回0表示失败，非0为成功
        private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);

        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);


        //构造函数文件路径初始
        public Model_ini_Class(string iniPath)
        {
            _iniPath = Path.GetFullPath(Application.StartupPath + "\\" + iniPath);
            //_iniPath = Path.GetFullPath(iniPath);
        }

        /// <读INI文件>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="NoText"></param>
        /// <文件路径="iniFilePath"></param>
        /// <returns></returns>
        public string Read_Ini(string Section, string Key, string sfc)
        {
            //解析字符
            try
            {
                Key = Key.Replace("\"", "");
                Key = Key.Replace(sfc, "");
            }
            catch (Exception e) {; }

            try
            {
                if (File.Exists(_iniPath))
                {
                    StringBuilder temp = new StringBuilder(1024);
                    GetPrivateProfileString(Section, Key, "", temp, 1024, _iniPath);
                    return temp.ToString();
                }
                else
                {
                    return String.Empty;
                }
            }
            catch (Exception e) { return String.Empty; }

        }
        public string Read_Ini(string Section, string Key)
        {
            //解析字符
            try
            {
                Key = Key.Replace("\"", "");
            }
            catch (Exception e) {; }

            try
            {
                if (File.Exists(_iniPath))
                {
                    StringBuilder temp = new StringBuilder(1024);
                    GetPrivateProfileString(Section, Key, "", temp, 1024, _iniPath);
                    return temp.ToString();
                }
                else
                {
                    return String.Empty;
                }
            }
            catch (Exception e) { return String.Empty; }

        }
        /// <写Ini文件>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        /// <param name="iniFilePath"></param>
        /// <returns></returns>
        public int Write_Ini(string Section, string Key, string Value)
        {
            try
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, _iniPath);
                if (OpStation == 0)
                { return 0; }
                else
                { return 1; }
            }
            catch (Exception e) { return 1; }
        }

        //序列化函数
        public void SerializeToXml<T>(string filePath, T obj)
        {
            try
            {
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    xs.Serialize(writer, obj);
                }
            }
            catch (Exception ex)
            {
            }
        }

        //反序列化函数
        public T DeserializeFromXml<T>(string filePath)
        {
            try
            {
                if (!System.IO.File.Exists(filePath))
                    throw new ArgumentNullException(filePath + " not Exists");

                using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    T ret = (T)xs.Deserialize(reader);
                    return ret;
                }
            }
            catch (Exception ex)
            {
                return default(T);
            }

        }

    }
    #endregion

}

